/**
 * Handles the class DBSalesOrderTest.
 * 
 * Author: Group 3
 * Date: 07-04-2014 Version: 1.0
 */
package testlayer;

import static org.junit.Assert.*;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;







import modellayer.Customer;
import modellayer.Invoice;
import modellayer.SalesOrder;







import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import dblayer.DBCustomer;
import dblayer.DBInvoice;
import dblayer.DBSalesOrder;
import dblayer.DBConnection;



public class DBSalesOrderTest {
	
	private static DBSalesOrder testSalesOrderDB = new DBSalesOrder();
	private static SalesOrder testSalesOrderInsert = new SalesOrder();
	private static DBConnection testConnection = DBConnection.getInstance();
	private static DBCustomer testCustomerDB = new DBCustomer();
	private static DBInvoice testInvoiceDB = new DBInvoice();
	
	
	@BeforeClass
	/**
	 * 
	 * @throws SQLException
	 * @throws UnsupportedEncodingException
	 * @throws FileNotFoundException
	 */
	public static void setUp() throws SQLException, UnsupportedEncodingException, FileNotFoundException
	{
		
		testConnection.resetDatabase();
		Invoice i = testInvoiceDB.selectSingleInvoice(1);
		Customer c = testCustomerDB.selectSingleCustomer(1, true);
		
		testSalesOrderInsert.setCustomer(c);
		testSalesOrderInsert.setCustomerID(c.getCustomerID());
		testSalesOrderInsert.setDate("01-02-2015");
		testSalesOrderInsert.setDeliveryDate("03-02-2015");
		testSalesOrderInsert.setDeliveryStatus("Ikke afleveret");
		testSalesOrderInsert.setInvoice(i);
		testSalesOrderInsert.setInvoiceID(i.getInvoiceID());
		testSalesOrderInsert.setSalesOrderID(1);
		
	}
	
	@AfterClass
	public static void tearDown() throws SQLException, FileNotFoundException {
		
		testConnection.resetDatabase();
	}
	
	/**
	 * Tests if you can get all the sales orders in the database
	 */
	@Test
	public void testGetAllSalesOrders() {
		assertNotNull(testSalesOrderDB.getAllSalesOrders(true));
	}

	/**
	 * Tests if you can get a single sales order in the database
	 * @throws SQLException
	 */
	@Test
	public void testSelectSingleSalesOrder() throws SQLException {
		assertNotNull(testSalesOrderDB.selectSingleSalesOrder(1, true));
	}

	/**
	 * Tests if you can insert a sales order into the database
	 * @throws SQLException
	 */
	@Test
	public void testInsertSalesOrder() throws SQLException {
		assertNotSame(-1, testSalesOrderDB.insertSalesOrder(testSalesOrderInsert));
	}

	/**
	 * Tests if you can update a sales order in the database
	 * @throws SQLException
	 */
	@Test
	public void testUpdateSalesOrder() throws SQLException {
		testSalesOrderDB.selectSingleSalesOrder(1, true).setDeliveryStatus("Afleveret");;
		assertNotSame(-1, testSalesOrderDB.updateSalesOrder(testSalesOrderDB.selectSingleSalesOrder(1, true)));
	}

	/**
	 * Tests if you can delete a sales order from the database
	 * @throws SQLException
	 */
	@Test
	public void testDeleteSalesOrder() throws SQLException {
		
		assertNotSame(-1, testSalesOrderDB.deleteSalesOrder(testSalesOrderDB.selectSingleSalesOrder(1, true)));

}

}
